package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDTO;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserLoginService;
import com.heima.utils.common.AppJwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class ApUserLoginServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserLoginService {
    @Autowired
    ApUserMapper apUserMapper;

    /**
     * app端用户登录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDTO dto) {
        //1:参数检验
        if(dto.getEquipmentId()==null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询登录用户
        if(StringUtils.isNotBlank(dto.getPhone()) &&
                StringUtils.isNotBlank(dto.getPassword())){
            ApUser apUser = apUserMapper.selectOne(Wrappers.<ApUser>lambdaQuery()
                    .eq(ApUser::getStatus, 0)
                    .eq(ApUser::getPhone, dto.getPhone()));
            if(apUser==null){
                return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"请检查手机号");
            }
            //数据库密码
            String password = apUser.getPassword();
             String newPassword = DigestUtils.md5DigestAsHex((dto.getPassword() + apUser.getSalt()).getBytes());
             if(!password.equals(newPassword)){
                 CustException.cust(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"手机号或密码错误");
             }
            Map<String,Object> map=new HashMap<>();
             apUser.setPassword("");
             apUser.setSalt("");
             map.put("token", AppJwtUtil.getToken(apUser.getId().longValue()));
             map.put("apUser",apUser);
             return  ResponseResult.okResult(map);

        }else {
            Map<String,Object> map=new HashMap<>();
            //通过设备id登录   userId 存 0
            map.put("token",AppJwtUtil.getToken(0L));
            return ResponseResult.okResult(map);
        }
    }
}
